package com.example.androidproject.app.deprecated.temporarily_deprecated_adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.androidproject.app.deprecated.ClassifyActivity2;
import com.example.androidproject.base.ListAsGridBaseAdapter;
import com.example.androidproject.R;
import com.example.androidproject.movie.activity.MovieDetailActivity;
import com.example.androidproject.movie.bean.ResultBeanData;
import com.example.androidproject.novel.bean.NovelBean;
import com.example.androidproject.utils.ResultsConstant;

import java.util.ArrayList;

public class ClassifyContentAdapter extends ListAsGridBaseAdapter {
    /**
     * data 是电影的数据
     */
    private ArrayList<ResultBeanData.DataBean> data = ResultsConstant.contentMovie;
    private ArrayList<NovelBean.DataBean> dataNovel = ResultsConstant.contentNovel;
    private final Context mContext;
    private final int typeFlag;

    public ClassifyContentAdapter(Context context, int TypeFlag) {
        super(context);
        this.mContext = context;
        this.typeFlag = TypeFlag;
    }

    public ClassifyContentAdapter(Context context, ArrayList<ResultBeanData.DataBean> data, int TypeFlag) {
        super(context);
        this.mContext = context;
        this.data = data;
        this.typeFlag = TypeFlag;
    }

    public ClassifyContentAdapter(ClassifyActivity2 context, ArrayList<NovelBean.DataBean> data, int TypeFlag) {
        super(context);
        this.mContext = context;
        this.dataNovel = data;
        this.typeFlag = TypeFlag;
    }

    @Override
    public int getItemCount() {
        int size = 0;
        if (typeFlag == 1) {
            size = data.size();
        }
        if (typeFlag == 2) {
            size = dataNovel.size();
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        if (typeFlag == 1) {
            ResultBeanData.DataBean dataItemMovie = data.get(position);
            return dataItemMovie;
        }
        if (typeFlag == 2) {
            NovelBean.DataBean dataItemNovel = dataNovel.get(position);
            return dataItemNovel;
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getItemView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = View.inflate(mContext, R.layout.classify_content_item, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_classify_content = view.findViewById(R.id.iv_classify_content);
            viewHolder.tv_classify_content = view.findViewById(R.id.tv_classify_content);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (typeFlag == 1) {
            Glide.with(mContext).load(data.get(i).getSrc()).into(viewHolder.iv_classify_content);
            viewHolder.tv_classify_content.setText(data.get(i).getName());
        }
        if (typeFlag == 2) {
            Glide.with(mContext).load(dataNovel.get(i).getImage()).into(viewHolder.iv_classify_content);
            viewHolder.tv_classify_content.setText(dataNovel.get(i).getTitle());
        }
        // 点击事件
        viewHolder.iv_classify_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到详情页的 需要一个flag 来判定 执行电影小说还是壁纸
                if (typeFlag == 1) {
                    // 跳转到电影详情页面
                    Intent intent = new Intent(mContext, MovieDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("data", data.get(i));
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
                if (typeFlag == 2) {
                    Toast.makeText(mContext, "" + i, Toast.LENGTH_SHORT).show();
                }
            }
        });
        return view;
    }

    public class ViewHolder {
        ImageView iv_classify_content;
        TextView tv_classify_content;
    }
}
